package com.electric.qrapp.utils.function

import android.content.Context
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.electric.qrapp.utils.system.Constants
import com.huantansheng.easyphotos.Builder.AlbumBuilder
import com.huantansheng.easyphotos.EasyPhotos
import com.huantansheng.easyphotos.callback.SelectCallback
import com.huantansheng.easyphotos.models.album.entity.Photo
import java.util.*

/**
 *@author created by Fanatic
 *@date 2021/9/22 21:11
 *@des 图片相册选择类
 */
class PhotoUtils {

    companion object {

        /**
         * 更多用法参考下方地址
         * https://github.com/HuanTanSheng/EasyPhotos/wiki/02-%E7%9B%B8%E6%9C%BA%E4%B8%8E%E7%9B%B8%E5%86%8C
         * @param photoCount Int 最大选择图片数量
         * @param camera Boolean  是否启动相机
         * @param album Boolean 是否启动相册
         * @param original Boolean 是否显示原图
         */
        fun getDefPhoto(
            fragment: Fragment?=null,
            context: Context?=null,
            photoCount: Int = 1,
            camera: Boolean = true,
            album: Boolean = true,
            original: Boolean = false,
            cancel: (() -> Unit)? = null,
            call: (List<Photo>?) -> Unit,
        ) {
            when {
                (camera && album) || album -> {
                    lateinit var createAlbum: AlbumBuilder;
                    fragment?.run {
                        createAlbum = EasyPhotos.createAlbum(fragment, camera, false, ImageLoader)
                    }
                    context?.run {
                        createAlbum = EasyPhotos.createAlbum(context as FragmentActivity, camera, false, ImageLoader)
                    }

                    createAlbum
                        .setPuzzleMenu(false)//拼图功能，传false即不显示
                        .setCount(photoCount)
                        .setOriginalMenu(false, original, null) //显示原图
                        .setFileProviderAuthority(Constants.Config.FILEPROVIDER)
                        .setMinWidth(500)//参数说明：最小宽度500px
                        .setGif(false)
                        .setVideo(false)
                        .setMinFileSize(1024 * 100L)//参数说明：最小文件大小10K
                        .start(PhotoResultListener(cancel, call));
                }
                camera -> {
                    EasyPhotos.createCamera(fragment, false)
                        .setFileProviderAuthority(Constants.Config.FILEPROVIDER)
                        .start(PhotoResultListener(cancel, call));
                }

            }
        }
    }

    class PhotoResultListener(
        private val cancel: (() -> Unit)? = null,
        val call: (List<Photo>?) -> Unit
    ) : SelectCallback() {
        override fun onResult(photos: ArrayList<Photo>?, isOriginal: Boolean) {
            val result =
                if (photos == null || photos.size == 0) null else photos;
            call.invoke(result)
        }

        override fun onCancel() {
            cancel?.invoke();
        }

    }
}